//
//  JXLMainTabBarController.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/19.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit
import SVProgressHUD
class JXLMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //添加自定义tabBar，利用kvc给只读属性赋值
        let jxlTabBar = JXLTabBar()
        
        //设置代理
        jxlTabBar.jxlDelegate = self
        
        //定义自定义tabBar的闭包
        //  在此使用self会产生循环引用, 解决办法使用[weak self]
        jxlTabBar.composeButtonCloser = { [weak self] in
            
            print("我是闭包调用过来的")
            
            if !JXLUserAccountViewModel.sharedUserAccountViewModel.isLogin {
               SVProgressHUD.showError(withStatus: "请先登录，～亲")
                return
            }
            
            //代码执行到此，表示已经登录
            if let target = self {
                //代码执行到此，表示已经登录
                let composeView = JXLComposeView()
                composeView.show(target: target)
            }
            
            
        }
        
        setValue(jxlTabBar, forKey: "tabBar")
        
       
        
        //添加子控制器
        addChildViewController(childController: JXLHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: JXLMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: JXLDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: JXLProFileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        //设置tabBar的图片和文字
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        //设置文字选中的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: UIControlState.selected)
       //设置文字大小，
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: UIControlState.normal)
        //设置文字内容
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
childController.title = title
        //创建导航控制器
        let navigationController = JXLNavigationController(rootViewController: childController)
        
        
        addChildViewController(navigationController)
        
    }

}

//用延展实现代理方法

extension JXLMainTabBarController:JXLTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
}

